{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e83d6c63-273a-46ba-8f56-bfc59721428d",
   "metadata": {},
   "source": [
    "# 1、Pandas入门\n",
    "## 1.1、pandas的Series\n",
    "### 1.1.1、Series的创建\n",
    "pandas把所有的二维数组都当成了表格，pandas的Series表示的是表格中的一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7a4c33ef-40af-40cc-8284-913ba91578b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    1.0\n",
      "1    2.0\n",
      "2    3.0\n",
      "3    4.0\n",
      "4    5.0\n",
      "5    6.0\n",
      "6    NaN\n",
      "7    3.0\n",
      "dtype: float64 (8,)\n",
      "A    1.0\n",
      "B    2.0\n",
      "C    3.0\n",
      "D    4.0\n",
      "E    5.0\n",
      "F    6.0\n",
      "G    NaN\n",
      "I    3.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "lst = [1,2,3,4,5,6,None,3]\n",
    "S = pd.Series(lst)\n",
    "print(S,S.shape)\n",
    "S = pd.Series(lst,index=list('ABCDEFGI'),dtype=float)\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "37b29ee5-e213-494e-9102-933a4201c1e3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([1, 2, 3, 4]), array([5, 6, 7, 8]), array([ 9, 10, 11, 12])]\n",
      "0       [1, 2, 3, 4]\n",
      "1       [5, 6, 7, 8]\n",
      "2    [9, 10, 11, 12]\n",
      "dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0       [1, 2, 3, 4]\n",
       "1       [5, 6, 7, 8]\n",
       "2    [9, 10, 11, 12]\n",
       "dtype: object"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "arr = np.array([\n",
    "    [1,2,3,4],\n",
    "    [5,6,7,8],\n",
    "    [9,10,11,12]\n",
    "])\n",
    "print(list(arr))\n",
    "# S = pd.Series(arr)\n",
    "S = pd.Series(list(arr))\n",
    "print(S)\n",
    "display(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfa06f52-622d-41c9-ab2b-128ca1494e04",
   "metadata": {},
   "source": [
    "### 1.1.2、pandas的底层数组的实现\n",
    "pands的底层数据是numpy的ndarray的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4f735ed2-d1ed-4346-b578-4a2269d31a5d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0     23\n",
      "1      4\n",
      "2      1\n",
      "3     31\n",
      "4      5\n",
      "5      1\n",
      "6     23\n",
      "7    515\n",
      "8      1\n",
      "dtype: int64 [ 23   4   1  31   5   1  23 515   1] <class 'numpy.ndarray'>\n",
      "[ 23   4   1  31   5   1  23 515   1] <class 'numpy.ndarray'>\n",
      "0     23\n",
      "1      4\n",
      "2    999\n",
      "3     31\n",
      "4      5\n",
      "5      1\n",
      "6     23\n",
      "7    515\n",
      "8      1\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "arr = np.array([23,4,1,31,5,1,23,515,1])\n",
    "S = pd.Series(arr)\n",
    "print(S,S.values,type(S.values))\n",
    "#在新版本中不建议使用values，因为不安全，建议使用S.to_numpy()来转换为numpy数组\n",
    "print(S.to_numpy(),type(S.to_numpy()))\n",
    "#修改了arr之后会同步的修改pandas中的数据\n",
    "arr[2] = 999\n",
    "print(S)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7b2400c-f1aa-472f-8f55-df5dbbe78063",
   "metadata": {},
   "source": [
    "### 1.1.3、pands来处理数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "de5fafa6-9ad9-4afc-aacd-5d61ffcfe7b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "D    4\n",
      "E    5\n",
      "dtype: int64\n",
      "****************************************************************************************************\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "S = pd.Series(arr,index=list('ABCDE'))\n",
    "print(S)\n",
    "print('*'*100)\n",
    "#第一种获取方式直接使用[]获取，此时是通过index来获取，如果没有加index可以s[0]来获取第一个元素，但是如果加了index之后，s[0]就无法获取了\n",
    "print(S['B'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0fecc1f7-da39-4ac3-b710-d363b2dbe38f",
   "metadata": {},
   "source": [
    "#### 使用iloc和loc来获取数据\n",
    "`iloc` 是 Pandas 里“**按位置**取数”的专用索引器（**i**nteger **loc**ation）。 `df.iloc[...]` 里的**中括号**，其实是 Python 的“**下标运算**”（`__getitem__`），并不是函数调用。换句话说：`df.iloc[行选择, 列选择]` 等价于 `df.iloc.__getitem__((行选择, 列选择))`。\\\n",
    "如果用小括号就是函数调用了（`df.iloc(...)`），那是不存在的接口。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "dbb13ac3-0394-4da5-be0b-58455c656545",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "D    4\n",
      "E    5\n",
      "dtype: int64\n",
      "****************************************************************************************************\n",
      "1\n",
      "B    2\n",
      "D    4\n",
      "dtype: int64\n",
      "E    5\n",
      "D    4\n",
      "C    3\n",
      "B    2\n",
      "A    1\n",
      "dtype: int64\n",
      "B    2\n",
      "dtype: int64\n",
      "4\n",
      "A    1\n",
      "B    2\n",
      "C    3\n",
      "D    4\n",
      "dtype: int64\n",
      "E    5\n",
      "D    4\n",
      "C    3\n",
      "B    2\n",
      "A    1\n",
      "dtype: int64\n",
      "A    1\n",
      "C    3\n",
      "E    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "arr = np.array([1,2,3,4,5])\n",
    "S = pd.Series(arr,index=list('ABCDE'))\n",
    "print(S)\n",
    "print('*'*100)\n",
    "#获取第0个位置的元素和S[0]不是一个一样\n",
    "print(S.iloc[0])\n",
    "print(S.iloc[1::2])\n",
    "print(S.iloc[::-1])\n",
    "#iloc就是numpy的切片，是包头不包尾的\n",
    "print(S.iloc[1:2])\n",
    "#loc表示使用名称来获取\n",
    "print(S.loc['D'])\n",
    "#loc中的切片是包头和包尾的\n",
    "print(S.loc['A':'D'])\n",
    "print(S.loc['E'::-1])\n",
    "print(S['A'::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d2746c42-79f8-4ecb-8842-e40ff267181d",
   "metadata": {},
   "source": [
    "### 1.1.4、Series的其他操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3f128ab5-f26e-43d6-918e-cd1c9c612cfc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A     80.0\n",
      "B     95.0\n",
      "C    100.0\n",
      "D     60.0\n",
      "E      NaN\n",
      "F     70.0\n",
      "dtype: float64\n",
      "81.0 100.0 60.0 16.73320053068151 80.0\n",
      "count      5.000000\n",
      "mean      81.000000\n",
      "std       16.733201\n",
      "min       60.000000\n",
      "25%       70.000000\n",
      "50%       80.000000\n",
      "75%       95.000000\n",
      "max      100.000000\n",
      "dtype: float64\n",
      "****************************************************************************************************\n",
      "<class 'pandas.core.series.Series'>\n",
      "Index: 6 entries, A to F\n",
      "Series name: None\n",
      "Non-Null Count  Dtype  \n",
      "--------------  -----  \n",
      "5 non-null      float64\n",
      "dtypes: float64(1)\n",
      "memory usage: 268.0+ bytes\n",
      "None\n",
      "****************************************************************************************************\n",
      "A    False\n",
      "B    False\n",
      "C    False\n",
      "D    False\n",
      "E     True\n",
      "F    False\n",
      "dtype: bool\n",
      "A     80.0\n",
      "B     95.0\n",
      "C    100.0\n",
      "D     60.0\n",
      "E     81.0\n",
      "F     70.0\n",
      "dtype: float64\n",
      "A     80.0\n",
      "B     95.0\n",
      "C    100.0\n",
      "D     60.0\n",
      "E      NaN\n",
      "F     70.0\n",
      "dtype: float64\n",
      "****************************************************************************************************\n",
      "A     80.0\n",
      "B     95.0\n",
      "C    100.0\n",
      "D     60.0\n",
      "F     70.0\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "S = pd.Series([80,85,90,60,np.nan,70],index=list('ABCDEF'))\n",
    "# S+=20\n",
    "#可以直接进行广播操作\n",
    "S[S>80]+=10\n",
    "print(S)\n",
    "print(S.mean(),S.max(),S.min(),S.std(),S.median())\n",
    "#看所有的统计信息\n",
    "print(S.describe())\n",
    "print('*'*100)\n",
    "#查询数据的基本信息\n",
    "print(S.info())\n",
    "print('*'*100)\n",
    "print(S.isna())\n",
    "#把空数据填充成为平均值\n",
    "print(S.fillna(S.mean()))\n",
    "#填充完成之后产生的是新数据，S是没有变的\n",
    "print(S)\n",
    "print('*'*100)\n",
    "#删除空数据\n",
    "print(S.dropna())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df13754c-2ca0-4757-939c-9a50c9eff1ba",
   "metadata": {},
   "source": [
    "## 1.2、Pandas处理二维数组\n",
    "### 1.2.1、创建DataFrame\n",
    "- 基于数组的方式创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "75bb0814-6057-411e-8c04-100b2b3dfd26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[92 49 86]\n",
      " [54 97 36]\n",
      " [89 60 70]\n",
      " [61 67 95]\n",
      " [75 49 40]\n",
      " [74 54 47]\n",
      " [70 63 33]\n",
      " [75 97 85]\n",
      " [81 94 55]\n",
      " [83 38 47]]\n",
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>92</td>\n",
       "      <td>49</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>89</td>\n",
       "      <td>60</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>61</td>\n",
       "      <td>67</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>75</td>\n",
       "      <td>49</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>74</td>\n",
       "      <td>54</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>70</td>\n",
       "      <td>63</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>75</td>\n",
       "      <td>97</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>81</td>\n",
       "      <td>94</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>83</td>\n",
       "      <td>38</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语\n",
       "A  92  49  86\n",
       "B  54  97  36\n",
       "C  89  60  70\n",
       "D  61  67  95\n",
       "E  75  49  40\n",
       "F  74  54  47\n",
       "G  70  63  33\n",
       "H  75  97  85\n",
       "I  81  94  55\n",
       "J  83  38  47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>92</td>\n",
       "      <td>49</td>\n",
       "      <td>86</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>89</td>\n",
       "      <td>60</td>\n",
       "      <td>70</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>61</td>\n",
       "      <td>67</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>75</td>\n",
       "      <td>49</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>74</td>\n",
       "      <td>54</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>70</td>\n",
       "      <td>63</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>75</td>\n",
       "      <td>97</td>\n",
       "      <td>85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>81</td>\n",
       "      <td>94</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>83</td>\n",
       "      <td>38</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语\n",
       "A  92  49  86\n",
       "B  54  97  36\n",
       "C  89  60  70\n",
       "D  61  67  95\n",
       "E  75  49  40\n",
       "F  74  54  47\n",
       "G  70  63  33\n",
       "H  75  97  85\n",
       "I  81  94  55\n",
       "J  83  38  47"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "arr = np.random.randint(30,100,(10,3))\n",
    "print(arr)\n",
    "print('*'*100)\n",
    "df = pd.DataFrame(arr)\n",
    "# print(df)\n",
    "df.index = list('ABCDEFGHIJ')\n",
    "df.columns = ['语文','数学','英语']\n",
    "display(df)\n",
    "print('*'*100)\n",
    "df = pd.DataFrame(arr,index=list('ABCDEFGHIJ'),columns=['语文','数学','英语'])\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5df6c226-4b89-4798-a647-a0ed874661dc",
   "metadata": {},
   "source": [
    " - 基于字典的方式来创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "635182dd-881f-44d2-a4c7-3cbcb060e3b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>张三</td>\n",
       "      <td>82</td>\n",
       "      <td>70</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>李四</td>\n",
       "      <td>53</td>\n",
       "      <td>80</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>王五</td>\n",
       "      <td>66</td>\n",
       "      <td>44</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>赵六</td>\n",
       "      <td>34</td>\n",
       "      <td>23</td>\n",
       "      <td>64</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名  语文  数学  英语\n",
       "A  张三  82  70  53\n",
       "B  李四  53  80  66\n",
       "C  王五  66  44  64\n",
       "D  赵六  34  23  64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>张三</td>\n",
       "      <td>53</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>李四</td>\n",
       "      <td>53</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>王五</td>\n",
       "      <td>53</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>赵六</td>\n",
       "      <td>53</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>朱琦</td>\n",
       "      <td>53</td>\n",
       "      <td>54</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  name  语文  数学  英语\n",
       "0   张三  53  54  65\n",
       "1   李四  53  54  65\n",
       "2   王五  53  54  65\n",
       "3   赵六  53  54  65\n",
       "4   朱琦  53  54  65"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "ach = {\n",
    "    '姓名':['张三','李四','王五','赵六'],\n",
    "    '语文':[82,53,66,34],\n",
    "    '数学':[70,80,44,23],\n",
    "    '英语':[53,66,64,64]\n",
    "}\n",
    "df = pd.DataFrame(ach,index=list('ABCD'))\n",
    "display(df)\n",
    "ach2 = [\n",
    "    {'name':'张三','语文':53,'数学':54,'英语':65},\n",
    "    {'name':'李四','语文':53,'数学':54,'英语':65},\n",
    "    {'name':'王五','语文':53,'数学':54,'英语':65},\n",
    "    {'name':'赵六','语文':53,'数学':54,'英语':65},\n",
    "    {'name':'朱琦','语文':53,'数学':54,'英语':65},\n",
    "]\n",
    "df = pd.DataFrame(ach2)\n",
    "df\n",
    "# print(\"-=--\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32350aa6-b95e-4d34-a6e4-8431d2961d13",
   "metadata": {},
   "source": [
    "### 1.2.2、DataFrame的基本属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "be7ae708-115f-4819-98de-20b9d9dac744",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>50</td>\n",
       "      <td>84</td>\n",
       "      <td>83</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>91</td>\n",
       "      <td>98</td>\n",
       "      <td>72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>58</td>\n",
       "      <td>87</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>38</td>\n",
       "      <td>82</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>82</td>\n",
       "      <td>52</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>53</td>\n",
       "      <td>97</td>\n",
       "      <td>96</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>30</td>\n",
       "      <td>58</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>93</td>\n",
       "      <td>58</td>\n",
       "      <td>35</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>46</td>\n",
       "      <td>58</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>31</td>\n",
       "      <td>93</td>\n",
       "      <td>94</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语\n",
       "A  50  84  83\n",
       "B  91  98  72\n",
       "C  58  87  74\n",
       "D  38  82  87\n",
       "E  82  52  47\n",
       "F  53  97  96\n",
       "G  30  58  80\n",
       "H  93  58  35\n",
       "I  46  58  50\n",
       "J  31  93  94"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10, 3)\n",
      "Index(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'], dtype='object')\n",
      "Index(['语文', '数学', '英语'], dtype='object')\n",
      "[[50 84 83]\n",
      " [91 98 72]\n",
      " [58 87 74]\n",
      " [38 82 87]\n",
      " [82 52 47]\n",
      " [53 97 96]\n",
      " [30 58 80]\n",
      " [93 58 35]\n",
      " [46 58 50]\n",
      " [31 93 94]]\n",
      "语文    int32\n",
      "数学    int32\n",
      "英语    int32\n",
      "dtype: object\n",
      "****************************************************************************************************\n",
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 10 entries, A to J\n",
      "Data columns (total 3 columns):\n",
      " #   Column  Non-Null Count  Dtype\n",
      "---  ------  --------------  -----\n",
      " 0   语文      10 non-null     int32\n",
      " 1   数学      10 non-null     int32\n",
      " 2   英语      10 non-null     int32\n",
      "dtypes: int32(3)\n",
      "memory usage: 200.0+ bytes\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "None"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>10.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>10.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>57.200000</td>\n",
       "      <td>76.700000</td>\n",
       "      <td>71.800000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>23.639891</td>\n",
       "      <td>18.190657</td>\n",
       "      <td>20.953918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>30.000000</td>\n",
       "      <td>52.000000</td>\n",
       "      <td>35.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>40.000000</td>\n",
       "      <td>58.000000</td>\n",
       "      <td>55.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>51.500000</td>\n",
       "      <td>83.000000</td>\n",
       "      <td>77.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>76.000000</td>\n",
       "      <td>91.500000</td>\n",
       "      <td>86.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>93.000000</td>\n",
       "      <td>98.000000</td>\n",
       "      <td>96.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              语文         数学         英语\n",
       "count  10.000000  10.000000  10.000000\n",
       "mean   57.200000  76.700000  71.800000\n",
       "std    23.639891  18.190657  20.953918\n",
       "min    30.000000  52.000000  35.000000\n",
       "25%    40.000000  58.000000  55.500000\n",
       "50%    51.500000  83.000000  77.000000\n",
       "75%    76.000000  91.500000  86.000000\n",
       "max    93.000000  98.000000  96.000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "arr = np.random.randint(30,100,(10,3))\n",
    "df = pd.DataFrame(arr,index=list('ABCDEFGHIJ'),columns=['语文','数学','英语'])\n",
    "display(df)\n",
    "#常用的属性\n",
    "print(df.shape)#行和列\n",
    "print(df.index)\n",
    "print(df.columns)\n",
    "print(df.to_numpy())\n",
    "print(df.dtypes)\n",
    "print('*'*100)\n",
    "display(df.info())\n",
    "print('*'*100)\n",
    "display(df.describe())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df7e7b14-4298-4d60-a4a4-6f30d7e9fe77",
   "metadata": {},
   "source": [
    "### 1.2.3、取值和访问\n",
    "df.iloc[行选择, 列选择] 等价于 df.iloc.__getitem__((行选择, 列选择))\n",
    "| 选择器类型   | 示例                                | 结果                          |\n",
    "| ------- | --------------------------------- | --------------------------- |\n",
    "| 单个整数    | `df.iloc[2, 1]`                   | 标量（第3行第2列）                  |\n",
    "| 切片      | `df.iloc[1:4, 0:2]`               | DataFrame，**左闭右开**（不含4、不含2） |\n",
    "| 整数列表/数组 | `df.iloc[[0, 3, 3], [2, 0]]`      | 可重排、可重复选择                   |\n",
    "| 布尔列表/数组 | `df.iloc[[True, False, True], :]` | 按位置布尔筛选（长度必须与轴长度一致）         |\n",
    "| 全部（冒号）  | `df.iloc[:, 1]`                   | 一列（Series）                  |\n",
    "| 负索引     | `df.iloc[-1, -2]`                 | 倒数第1行、第2列                   |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "9a3cad6c-2959-4210-acd8-ef3cee52379b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A    46\n",
      "B    82\n",
      "C    38\n",
      "D    57\n",
      "E    40\n",
      "F    81\n",
      "G    69\n",
      "H    80\n",
      "I    73\n",
      "J    34\n",
      "Name: 语文, dtype: int32\n",
      "46\n",
      "   数学\n",
      "A  86\n",
      "B  75\n",
      "C  36\n",
      "D  63\n",
      "E  38\n",
      "F  97\n",
      "G  77\n",
      "H  52\n",
      "I  43\n",
      "J  49 (10, 1)\n",
      "语文    46\n",
      "数学    86\n",
      "英语    34\n",
      "Name: A, dtype: int32 (3,)\n",
      "A    34\n",
      "B    44\n",
      "C    38\n",
      "D    83\n",
      "E    75\n",
      "F    34\n",
      "G    69\n",
      "H    66\n",
      "I    65\n",
      "J    43\n",
      "Name: 英语, dtype: int32\n",
      "34\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "arr = np.random.randint(30,100,(10,3))\n",
    "df = pd.DataFrame(arr,index=list('ABCDEFGHIJ'),columns=['语文','数学','英语'])\n",
    "print(df['语文'])\n",
    "print(df.loc['A','语文'])\n",
    "print(df.loc[:,['数学']],df.loc[:,['数学']].shape)\n",
    "print(df.iloc[0],df.iloc[0].shape)\n",
    "#取出最后一列\n",
    "print(df.iloc[:,2])\n",
    "#A同学的英语成绩\n",
    "print(df.iloc[0,2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7a03c2f-78dd-4038-9eb7-102c1832301a",
   "metadata": {},
   "source": [
    "### 1.2.4、取值练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "bc85dd44-956b-4433-850d-98a9064ce424",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>物理</th>\n",
       "      <th>Python</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>100</td>\n",
       "      <td>40</td>\n",
       "      <td>80</td>\n",
       "      <td>42</td>\n",
       "      <td>75</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>80</td>\n",
       "      <td>90</td>\n",
       "      <td>90</td>\n",
       "      <td>52</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>90</td>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "      <td>63</td>\n",
       "      <td>65</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     语文  数学  英语  物理  Python\n",
       "张三  100  40  80  42      75\n",
       "李四   80  90  90  52      43\n",
       "王五   90  60  50  63      65"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "80\n",
      "语文        90\n",
      "数学        60\n",
      "英语        50\n",
      "物理        63\n",
      "Python    65\n",
      "Name: 王五, dtype: int64 (5,)\n",
      "****************************************************************************************************\n",
      "张三    80\n",
      "李四    90\n",
      "王五    50\n",
      "Name: 英语, dtype: int64 (3,)\n",
      "    英语\n",
      "张三  80\n",
      "李四  90\n",
      "王五  50 (3, 1)\n",
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>英语</th>\n",
       "      <th>语文</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>80</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>50</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>50</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    英语   语文\n",
       "张三  80  100\n",
       "王五  50   90\n",
       "王五  50   90"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "张三     True\n",
       "李四    False\n",
       "王五     True\n",
       "Name: 语文, dtype: bool"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "张三     True\n",
       "李四    False\n",
       "王五     True\n",
       "Name: 语文, dtype: bool"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n",
      "40\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>90</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>60</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    数学  英语\n",
       "李四  90  90\n",
       "王五  60  50"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>数学</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    数学\n",
       "张三  40\n",
       "李四  90\n",
       "王五  60"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "arr = np.array([\n",
    "    [100,40,80,42,75],\n",
    "    [80,90,90,52,43],\n",
    "    [90,60,50,63,65]\n",
    "])\n",
    "df = pd.DataFrame(arr,index=['张三','李四','王五'],columns=['语文','数学','英语','物理','Python'])\n",
    "display(df)\n",
    "#返回一个元素\n",
    "print(df.iloc[0,2])\n",
    "print(df.iloc[2],df.iloc[2].shape)\n",
    "print('*'*100)\n",
    "print(df.iloc[:,2],df.iloc[:,2].shape)\n",
    "#转换为二维数组的列\n",
    "print(df.iloc[:,[2]],df.iloc[:,[2]].shape)\n",
    "print('*'*100)\n",
    "#支持花式索引的\n",
    "display(df.iloc[[0,2,2],[2,0]])\n",
    "#同样支持布尔索引的\n",
    "display(df['语文']>80)\n",
    "#numpy的使用\n",
    "display(df.iloc[:,0]>80)\n",
    "#可以直接使用loc\n",
    "print('*'*100)\n",
    "print(df.loc['张三','数学'])\n",
    "display(df.loc[['李四','王五'],['数学','英语']])\n",
    "display(df.loc[:,['数学']])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e061d2aa-d6e6-4f61-b3c0-771983973090",
   "metadata": {},
   "source": [
    "### 1.2.5、增删改操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "8754c22e-a9ae-441d-a82c-5aa9f495b490",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>38</td>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>44</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>45</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>46</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>91</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语\n",
       "A  38  54  97\n",
       "B  78  40  82\n",
       "C  83  96  44\n",
       "D  64  54  45\n",
       "E  90  88  46\n",
       "F  39  32  57\n",
       "G  34  61  31\n",
       "H  43  34  89\n",
       "I  97  37  79\n",
       "J  77  95  91"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>38</td>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>189</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>44</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>45</td>\n",
       "      <td>163</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>46</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>57</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>31</td>\n",
       "      <td>126</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>89</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>79</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>91</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语   总分  平均分\n",
       "A  38  54  97  189   63\n",
       "B  78  40  82  200   66\n",
       "C  83  96  44  223   74\n",
       "D  64  54  45  163   54\n",
       "E  90  88  46  224   74\n",
       "F  39  32  57  128   42\n",
       "G  34  61  31  126   42\n",
       "H  43  34  89  166   55\n",
       "I  97  37  79  213   71\n",
       "J  77  95  91  263   87"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   语文  数学   总分  平均分\n",
      "A  38  54  189   63\n",
      "B  78  40  200   66\n",
      "C  83  96  223   74\n",
      "D  64  54  163   54\n",
      "E  90  88  224   74\n",
      "F  39  32  128   42\n",
      "G  34  61  126   42\n",
      "H  43  34  166   55\n",
      "I  97  37  213   71\n",
      "J  77  95  263   87\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>126</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学   总分  平均分\n",
       "B  78  40  200   66\n",
       "C  83  96  223   74\n",
       "E  90  88  224   74\n",
       "F  39  32  128   42\n",
       "G  34  61  126   42\n",
       "H  43  34  166   55\n",
       "I  97  37  213   71\n",
       "J  77  95  263   87"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['G'], dtype='object')\n",
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学   总分  平均分\n",
       "B  78  40  200   66\n",
       "C  83  96  223   74\n",
       "E  90  88  224   74\n",
       "F  39  32  128   42\n",
       "H  43  34  166   55\n",
       "I  97  37  213   71\n",
       "J  77  95  263   87"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   语文  数学  英语   总分  平均分\n",
      "A  99  54  97  189   63\n",
      "B  78  40  82  200   66\n",
      "C  83  96  44  223   74\n",
      "D  64  54  45  163   54\n",
      "E  90  88  46  224   74\n",
      "F  39  32  57  128   42\n",
      "G  34  61  31  126   42\n",
      "H  43  34  89  166   55\n",
      "I  97  37  79  213   71\n",
      "J  77  95  91  263   87\n",
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>chinese</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>99</td>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>189</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>44</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>45</td>\n",
       "      <td>163</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>46</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>57</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>31</td>\n",
       "      <td>126</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>89</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>79</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>91</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   chinese  数学  英语   总分  平均分\n",
       "A       99  54  97  189   63\n",
       "B       78  40  82  200   66\n",
       "C       83  96  44  223   74\n",
       "D       64  54  45  163   54\n",
       "E       90  88  46  224   74\n",
       "F       39  32  57  128   42\n",
       "G       34  61  31  126   42\n",
       "H       43  34  89  166   55\n",
       "I       97  37  79  213   71\n",
       "J       77  95  91  263   87"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "np.random.seed(100)\n",
    "arr = np.random.randint(30,100,(10,3))\n",
    "df = pd.DataFrame(arr,index=list('ABCDEFGHIJ'),columns=['语文','数学','英语'])\n",
    "display(df)\n",
    "print('*'*100)\n",
    "#添加操作\n",
    "t = df[['语文','数学','英语']].sum(axis=1)\n",
    "df['总分'] = t\n",
    "df['平均分'] = (t/3).astype(int)\n",
    "# df['test'] = 100\n",
    "display(df)\n",
    "#删除一列\n",
    "df1 = df.drop(columns=['英语'],inplace=False)\n",
    "print(df1)\n",
    "#删除AD两个数据\n",
    "df1.drop(index=list('AD'),inplace=True)\n",
    "display(df1)\n",
    "# print(df1['语文']==34)\n",
    "# print((df1['语文']==34).index)\n",
    "idx = df1[df1['语文']==34].index\n",
    "#先通过布尔检索得到相应的索引\n",
    "print(idx)\n",
    "df1.drop(idx,inplace=True)\n",
    "print('*'*100)\n",
    "display(df1)\n",
    "#修改\n",
    "df.loc['A','语文'] = 99\n",
    "print(df)\n",
    "df.rename(columns={'语文':'chinese'},inplace=True)\n",
    "print('*'*100)\n",
    "display(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26158c09-c54c-476c-a3bb-c793bf537527",
   "metadata": {},
   "source": [
    "### 1.2.5、排序统计等操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "d0bbea80-504e-4d5a-9eaa-b3b65a655745",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>38</td>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>189</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>44</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>45</td>\n",
       "      <td>163</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>46</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>57</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>31</td>\n",
       "      <td>126</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>89</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>79</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>91</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语   总分  平均分\n",
       "A  38  54  97  189   63\n",
       "B  78  40  82  200   66\n",
       "C  83  96  44  223   74\n",
       "D  64  54  45  163   54\n",
       "E  90  88  46  224   74\n",
       "F  39  32  57  128   42\n",
       "G  34  61  31  126   42\n",
       "H  43  34  89  166   55\n",
       "I  97  37  79  213   71\n",
       "J  77  95  91  263   87"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>91</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>46</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>44</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>79</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>38</td>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>189</td>\n",
       "      <td>63</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>89</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>45</td>\n",
       "      <td>163</td>\n",
       "      <td>54</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>57</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>31</td>\n",
       "      <td>126</td>\n",
       "      <td>42</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语   总分  平均分\n",
       "J  77  95  91  263   87\n",
       "E  90  88  46  224   74\n",
       "C  83  96  44  223   74\n",
       "I  97  37  79  213   71\n",
       "B  78  40  82  200   66\n",
       "A  38  54  97  189   63\n",
       "H  43  34  89  166   55\n",
       "D  64  54  45  163   54\n",
       "F  39  32  57  128   42\n",
       "G  34  61  31  126   42"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n",
      "语文      64.3\n",
      "数学      59.1\n",
      "英语      66.1\n",
      "总分     189.5\n",
      "平均分     62.8\n",
      "dtype: float64\n",
      "A     88.2\n",
      "B     93.2\n",
      "C    104.0\n",
      "D     76.0\n",
      "E    104.4\n",
      "F     59.6\n",
      "G     58.8\n",
      "H     77.4\n",
      "I     99.4\n",
      "J    122.6\n",
      "dtype: float64\n",
      "语文    97\n",
      "数学    96\n",
      "dtype: int32\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>10.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>10.0000</td>\n",
       "      <td>10.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>64.300000</td>\n",
       "      <td>59.100000</td>\n",
       "      <td>66.100000</td>\n",
       "      <td>189.5000</td>\n",
       "      <td>62.800000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>23.879559</td>\n",
       "      <td>25.295806</td>\n",
       "      <td>23.969657</td>\n",
       "      <td>44.0284</td>\n",
       "      <td>14.596803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>34.000000</td>\n",
       "      <td>32.000000</td>\n",
       "      <td>31.000000</td>\n",
       "      <td>126.0000</td>\n",
       "      <td>42.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>40.000000</td>\n",
       "      <td>37.750000</td>\n",
       "      <td>45.250000</td>\n",
       "      <td>163.7500</td>\n",
       "      <td>54.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>70.500000</td>\n",
       "      <td>54.000000</td>\n",
       "      <td>68.000000</td>\n",
       "      <td>194.5000</td>\n",
       "      <td>64.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>81.750000</td>\n",
       "      <td>81.250000</td>\n",
       "      <td>87.250000</td>\n",
       "      <td>220.5000</td>\n",
       "      <td>73.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>97.000000</td>\n",
       "      <td>96.000000</td>\n",
       "      <td>97.000000</td>\n",
       "      <td>263.0000</td>\n",
       "      <td>87.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              语文         数学         英语        总分        平均分\n",
       "count  10.000000  10.000000  10.000000   10.0000  10.000000\n",
       "mean   64.300000  59.100000  66.100000  189.5000  62.800000\n",
       "std    23.879559  25.295806  23.969657   44.0284  14.596803\n",
       "min    34.000000  32.000000  31.000000  126.0000  42.000000\n",
       "25%    40.000000  37.750000  45.250000  163.7500  54.250000\n",
       "50%    70.500000  54.000000  68.000000  194.5000  64.500000\n",
       "75%    81.750000  81.250000  87.250000  220.5000  73.250000\n",
       "max    97.000000  96.000000  97.000000  263.0000  87.000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "****************************************************************************************************\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>语文</th>\n",
       "      <th>数学</th>\n",
       "      <th>英语</th>\n",
       "      <th>总分</th>\n",
       "      <th>平均分</th>\n",
       "      <th>班级</th>\n",
       "      <th>性别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>38</td>\n",
       "      <td>54</td>\n",
       "      <td>97</td>\n",
       "      <td>189</td>\n",
       "      <td>63</td>\n",
       "      <td>一班</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>78</td>\n",
       "      <td>40</td>\n",
       "      <td>82</td>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "      <td>二班</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>83</td>\n",
       "      <td>96</td>\n",
       "      <td>44</td>\n",
       "      <td>223</td>\n",
       "      <td>74</td>\n",
       "      <td>三班</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>64</td>\n",
       "      <td>54</td>\n",
       "      <td>45</td>\n",
       "      <td>163</td>\n",
       "      <td>54</td>\n",
       "      <td>一班</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>E</th>\n",
       "      <td>90</td>\n",
       "      <td>88</td>\n",
       "      <td>46</td>\n",
       "      <td>224</td>\n",
       "      <td>74</td>\n",
       "      <td>二班</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>F</th>\n",
       "      <td>39</td>\n",
       "      <td>32</td>\n",
       "      <td>57</td>\n",
       "      <td>128</td>\n",
       "      <td>42</td>\n",
       "      <td>三班</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>G</th>\n",
       "      <td>34</td>\n",
       "      <td>61</td>\n",
       "      <td>31</td>\n",
       "      <td>126</td>\n",
       "      <td>42</td>\n",
       "      <td>二班</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>H</th>\n",
       "      <td>43</td>\n",
       "      <td>34</td>\n",
       "      <td>89</td>\n",
       "      <td>166</td>\n",
       "      <td>55</td>\n",
       "      <td>三班</td>\n",
       "      <td>M</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>I</th>\n",
       "      <td>97</td>\n",
       "      <td>37</td>\n",
       "      <td>79</td>\n",
       "      <td>213</td>\n",
       "      <td>71</td>\n",
       "      <td>一班</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>J</th>\n",
       "      <td>77</td>\n",
       "      <td>95</td>\n",
       "      <td>91</td>\n",
       "      <td>263</td>\n",
       "      <td>87</td>\n",
       "      <td>二班</td>\n",
       "      <td>F</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   语文  数学  英语   总分  平均分  班级 性别\n",
       "A  38  54  97  189   63  一班  M\n",
       "B  78  40  82  200   66  二班  M\n",
       "C  83  96  44  223   74  三班  F\n",
       "D  64  54  45  163   54  一班  M\n",
       "E  90  88  46  224   74  二班  M\n",
       "F  39  32  57  128   42  三班  F\n",
       "G  34  61  31  126   42  二班  M\n",
       "H  43  34  89  166   55  三班  M\n",
       "I  97  37  79  213   71  一班  F\n",
       "J  77  95  91  263   87  二班  F"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "班级\n",
      "一班    188.333333\n",
      "三班    172.333333\n",
      "二班    203.250000\n",
      "Name: 总分, dtype: float64\n",
      "性别\n",
      "F    4\n",
      "M    6\n",
      "Name: 语文, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "np.random.seed(100)\n",
    "arr = np.random.randint(30,100,(10,3))\n",
    "df = pd.DataFrame(arr,index=list('ABCDEFGHIJ'),columns=['语文','数学','英语'])\n",
    "t = df[['语文','数学','英语']].sum(axis=1)\n",
    "df['总分'] = t\n",
    "df['平均分'] = (t/3).astype(int)\n",
    "display(df)\n",
    "print('*'*100)\n",
    "#排序\n",
    "display(df.sort_values(by='总分',ascending=False))\n",
    "#筛选\n",
    "print('*'*100)\n",
    "# display(df[df['语文']>80])\n",
    "df[(df['语文']>70)&(df['数学']<80)]\n",
    "#统计\n",
    "print(df.mean())\n",
    "print(df.mean(axis=1))\n",
    "print(df[['语文','数学']].max())\n",
    "display(df.describe())\n",
    "#分组统计\n",
    "df['班级'] = ['一班','二班','三班','一班','二班','三班','二班','三班','一班','二班']\n",
    "df['性别'] = ['M','M','F','M','M','F','M','M','F','F']\n",
    "print('*'*100)\n",
    "display(df)\n",
    "#统计班级的总分的平均分\n",
    "print(df.groupby('班级')['总分'].mean())\n",
    "#统计男女生的人数\n",
    "print(df.groupby('性别')['语文'].count())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e9e71323-afb0-45c3-b189-c55fb274eb2c",
   "metadata": {},
   "source": [
    "## 1.3、文件处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "id": "2feb758e-ade0-4062-ad12-b39735caa71b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>student_id</th>\n",
       "      <th>name</th>\n",
       "      <th>gender</th>\n",
       "      <th>class</th>\n",
       "      <th>age</th>\n",
       "      <th>register_date</th>\n",
       "      <th>city</th>\n",
       "      <th>club</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "      <th>English</th>\n",
       "      <th>Physics</th>\n",
       "      <th>Chemistry</th>\n",
       "      <th>Biology</th>\n",
       "      <th>absence_count</th>\n",
       "      <th>height_cm</th>\n",
       "      <th>weight_kg</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>70</th>\n",
       "      <td>23071</td>\n",
       "      <td>金坤杰</td>\n",
       "      <td>F</td>\n",
       "      <td>3B</td>\n",
       "      <td>10</td>\n",
       "      <td>2024-08-13</td>\n",
       "      <td>zhaotong</td>\n",
       "      <td>NaN</td>\n",
       "      <td>75.7</td>\n",
       "      <td>70.3</td>\n",
       "      <td>78.4</td>\n",
       "      <td>56.0</td>\n",
       "      <td>80.6</td>\n",
       "      <td>69.6</td>\n",
       "      <td>2.0</td>\n",
       "      <td>159.4</td>\n",
       "      <td>39.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>115</th>\n",
       "      <td>23116</td>\n",
       "      <td>华静杰</td>\n",
       "      <td>M</td>\n",
       "      <td>4B</td>\n",
       "      <td>11</td>\n",
       "      <td>2024-09-26</td>\n",
       "      <td>Kunming</td>\n",
       "      <td>书法</td>\n",
       "      <td>84.9</td>\n",
       "      <td>93.9</td>\n",
       "      <td>91.8</td>\n",
       "      <td>96.7</td>\n",
       "      <td>91.0</td>\n",
       "      <td>77.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>159.5</td>\n",
       "      <td>40.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>23038</td>\n",
       "      <td>秦杰丽</td>\n",
       "      <td>F</td>\n",
       "      <td>3B</td>\n",
       "      <td>10</td>\n",
       "      <td>2024-01-08</td>\n",
       "      <td>NaN</td>\n",
       "      <td>足球</td>\n",
       "      <td>81.6</td>\n",
       "      <td>73.2</td>\n",
       "      <td>94.1</td>\n",
       "      <td>61.4</td>\n",
       "      <td>65.1</td>\n",
       "      <td>68.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>154.3</td>\n",
       "      <td>42.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>23047</td>\n",
       "      <td>韩宁骁</td>\n",
       "      <td>M</td>\n",
       "      <td>2A</td>\n",
       "      <td>10</td>\n",
       "      <td>2024-06-24</td>\n",
       "      <td>zhaotong</td>\n",
       "      <td>NaN</td>\n",
       "      <td>74.8</td>\n",
       "      <td>85.9</td>\n",
       "      <td>74.8</td>\n",
       "      <td>84.7</td>\n",
       "      <td>82.0</td>\n",
       "      <td>80.8</td>\n",
       "      <td>3.0</td>\n",
       "      <td>153.9</td>\n",
       "      <td>32.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>23100</td>\n",
       "      <td>郑涛昕</td>\n",
       "      <td>女</td>\n",
       "      <td>2B</td>\n",
       "      <td>9</td>\n",
       "      <td>2023-12-10</td>\n",
       "      <td>zhaotong</td>\n",
       "      <td>NaN</td>\n",
       "      <td>77.0</td>\n",
       "      <td>96.7</td>\n",
       "      <td>84.9</td>\n",
       "      <td>94.1</td>\n",
       "      <td>98.1</td>\n",
       "      <td>79.5</td>\n",
       "      <td>4.0</td>\n",
       "      <td>147.7</td>\n",
       "      <td>40.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>23027</td>\n",
       "      <td>卫博琪</td>\n",
       "      <td>F</td>\n",
       "      <td>3B</td>\n",
       "      <td>10</td>\n",
       "      <td>2024-02-03</td>\n",
       "      <td>昭通</td>\n",
       "      <td>合唱</td>\n",
       "      <td>77.6</td>\n",
       "      <td>84.8</td>\n",
       "      <td>73.5</td>\n",
       "      <td>81.2</td>\n",
       "      <td>75.2</td>\n",
       "      <td>82.4</td>\n",
       "      <td>NaN</td>\n",
       "      <td>164.3</td>\n",
       "      <td>44.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>23105</td>\n",
       "      <td>韩坤杰</td>\n",
       "      <td>M</td>\n",
       "      <td>4A</td>\n",
       "      <td>12</td>\n",
       "      <td>2024-04-12</td>\n",
       "      <td>Kunming</td>\n",
       "      <td>篮球</td>\n",
       "      <td>76.1</td>\n",
       "      <td>85.0</td>\n",
       "      <td>78.5</td>\n",
       "      <td>76.3</td>\n",
       "      <td>80.3</td>\n",
       "      <td>87.1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>165.2</td>\n",
       "      <td>53.7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>23103</td>\n",
       "      <td>韩超博</td>\n",
       "      <td>女</td>\n",
       "      <td>4B</td>\n",
       "      <td>12</td>\n",
       "      <td>2024-07-20</td>\n",
       "      <td>Kunming</td>\n",
       "      <td>书法</td>\n",
       "      <td>81.4</td>\n",
       "      <td>74.3</td>\n",
       "      <td>72.2</td>\n",
       "      <td>81.9</td>\n",
       "      <td>75.0</td>\n",
       "      <td>73.3</td>\n",
       "      <td>3.0</td>\n",
       "      <td>158.2</td>\n",
       "      <td>49.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>23022</td>\n",
       "      <td>褚坤璐</td>\n",
       "      <td>M</td>\n",
       "      <td>3A</td>\n",
       "      <td>11</td>\n",
       "      <td>2024-04-25</td>\n",
       "      <td>zhaotong</td>\n",
       "      <td>书法</td>\n",
       "      <td>65.6</td>\n",
       "      <td>90.6</td>\n",
       "      <td>76.0</td>\n",
       "      <td>82.5</td>\n",
       "      <td>72.3</td>\n",
       "      <td>86.3</td>\n",
       "      <td>4.0</td>\n",
       "      <td>159.9</td>\n",
       "      <td>45.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>23034</td>\n",
       "      <td>卫超桐</td>\n",
       "      <td>女</td>\n",
       "      <td>4B</td>\n",
       "      <td>12</td>\n",
       "      <td>2024-06-11</td>\n",
       "      <td>曲靖</td>\n",
       "      <td>合唱</td>\n",
       "      <td>69.6</td>\n",
       "      <td>78.7</td>\n",
       "      <td>71.7</td>\n",
       "      <td>82.2</td>\n",
       "      <td>74.8</td>\n",
       "      <td>84.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>162.1</td>\n",
       "      <td>43.9</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     student_id name gender class  age register_date      city club  Chinese  \\\n",
       "70        23071  金坤杰      F    3B   10    2024-08-13  zhaotong  NaN     75.7   \n",
       "115       23116  华静杰      M    4B   11    2024-09-26   Kunming   书法     84.9   \n",
       "37        23038  秦杰丽      F    3B   10    2024-01-08       NaN   足球     81.6   \n",
       "46        23047  韩宁骁      M    2A   10    2024-06-24  zhaotong  NaN     74.8   \n",
       "99        23100  郑涛昕      女    2B    9    2023-12-10  zhaotong  NaN     77.0   \n",
       "26        23027  卫博琪      F    3B   10    2024-02-03        昭通   合唱     77.6   \n",
       "104       23105  韩坤杰      M    4A   12    2024-04-12   Kunming   篮球     76.1   \n",
       "102       23103  韩超博      女    4B   12    2024-07-20   Kunming   书法     81.4   \n",
       "21        23022  褚坤璐      M    3A   11    2024-04-25  zhaotong   书法     65.6   \n",
       "33        23034  卫超桐      女    4B   12    2024-06-11        曲靖   合唱     69.6   \n",
       "\n",
       "     Math  English  Physics  Chemistry  Biology  absence_count  height_cm  \\\n",
       "70   70.3     78.4     56.0       80.6     69.6            2.0      159.4   \n",
       "115  93.9     91.8     96.7       91.0     77.5            3.0      159.5   \n",
       "37   73.2     94.1     61.4       65.1     68.7            3.0      154.3   \n",
       "46   85.9     74.8     84.7       82.0     80.8            3.0      153.9   \n",
       "99   96.7     84.9     94.1       98.1     79.5            4.0      147.7   \n",
       "26   84.8     73.5     81.2       75.2     82.4            NaN      164.3   \n",
       "104  85.0     78.5     76.3       80.3     87.1            1.0      165.2   \n",
       "102  74.3     72.2     81.9       75.0     73.3            3.0      158.2   \n",
       "21   90.6     76.0     82.5       72.3     86.3            4.0      159.9   \n",
       "33   78.7     71.7     82.2       74.8     84.8            0.0      162.1   \n",
       "\n",
       "     weight_kg  \n",
       "70        39.6  \n",
       "115       40.5  \n",
       "37        42.2  \n",
       "46        32.7  \n",
       "99        40.7  \n",
       "26        44.0  \n",
       "104       53.7  \n",
       "102       49.6  \n",
       "21        45.6  \n",
       "33        43.9  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>student_id</th>\n",
       "      <th>name</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "      <th>English</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>23001</td>\n",
       "      <td>褚超琪</td>\n",
       "      <td>83.2</td>\n",
       "      <td>104.5</td>\n",
       "      <td>999.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>23002</td>\n",
       "      <td>孙明丽</td>\n",
       "      <td>74.5</td>\n",
       "      <td>NaN</td>\n",
       "      <td>81.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>23003</td>\n",
       "      <td>冯鑫芸</td>\n",
       "      <td>NaN</td>\n",
       "      <td>58.5</td>\n",
       "      <td>83.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>23004</td>\n",
       "      <td>孔凯霖</td>\n",
       "      <td>76.7</td>\n",
       "      <td>69.7</td>\n",
       "      <td>81.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>23005</td>\n",
       "      <td>杨旭豪</td>\n",
       "      <td>67.9</td>\n",
       "      <td>66.8</td>\n",
       "      <td>75.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>116</th>\n",
       "      <td>23117</td>\n",
       "      <td>曹瀚霖</td>\n",
       "      <td>73.0</td>\n",
       "      <td>96.6</td>\n",
       "      <td>72.4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>23118</td>\n",
       "      <td>冯楠玥</td>\n",
       "      <td>81.4</td>\n",
       "      <td>85.8</td>\n",
       "      <td>70.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>118</th>\n",
       "      <td>23119</td>\n",
       "      <td>陶鑫璐</td>\n",
       "      <td>93.5</td>\n",
       "      <td>95.3</td>\n",
       "      <td>87.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>119</th>\n",
       "      <td>23120</td>\n",
       "      <td>赵宁睿</td>\n",
       "      <td>84.7</td>\n",
       "      <td>72.2</td>\n",
       "      <td>76.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>120</th>\n",
       "      <td>23011</td>\n",
       "      <td>孔雯璐</td>\n",
       "      <td>90.0</td>\n",
       "      <td>64.7</td>\n",
       "      <td>67.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>121 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     student_id   name  Chinese   Math  English\n",
       "0         23001    褚超琪     83.2  104.5    999.0\n",
       "1         23002    孙明丽     74.5    NaN     81.3\n",
       "2         23003   冯鑫芸       NaN   58.5     83.9\n",
       "3         23004    孔凯霖     76.7   69.7     81.6\n",
       "4         23005    杨旭豪     67.9   66.8     75.1\n",
       "..          ...    ...      ...    ...      ...\n",
       "116       23117    曹瀚霖     73.0   96.6     72.4\n",
       "117       23118    冯楠玥     81.4   85.8     70.6\n",
       "118       23119    陶鑫璐     93.5   95.3     87.8\n",
       "119       23120    赵宁睿     84.7   72.2     76.6\n",
       "120       23011    孔雯璐     90.0   64.7     67.8\n",
       "\n",
       "[121 rows x 5 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 121 entries, 0 to 120\n",
      "Data columns (total 5 columns):\n",
      " #   Column      Non-Null Count  Dtype  \n",
      "---  ------      --------------  -----  \n",
      " 0   student_id  121 non-null    int64  \n",
      " 1   name        121 non-null    object \n",
      " 2   Chinese     115 non-null    float64\n",
      " 3   Math        112 non-null    float64\n",
      " 4   English     121 non-null    float64\n",
      "dtypes: float64(3), int64(1), object(1)\n",
      "memory usage: 4.9+ KB\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "None"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>student_id</th>\n",
       "      <th>Chinese</th>\n",
       "      <th>Math</th>\n",
       "      <th>English</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>121.000000</td>\n",
       "      <td>115.000000</td>\n",
       "      <td>112.000000</td>\n",
       "      <td>121.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>23060.090909</td>\n",
       "      <td>80.170435</td>\n",
       "      <td>80.370536</td>\n",
       "      <td>93.771901</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>34.930884</td>\n",
       "      <td>8.977525</td>\n",
       "      <td>11.157364</td>\n",
       "      <td>118.098218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>23001.000000</td>\n",
       "      <td>58.800000</td>\n",
       "      <td>54.600000</td>\n",
       "      <td>57.200000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>23030.000000</td>\n",
       "      <td>74.200000</td>\n",
       "      <td>71.875000</td>\n",
       "      <td>73.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>23060.000000</td>\n",
       "      <td>79.300000</td>\n",
       "      <td>81.900000</td>\n",
       "      <td>78.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>23090.000000</td>\n",
       "      <td>85.500000</td>\n",
       "      <td>88.125000</td>\n",
       "      <td>83.800000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>23120.000000</td>\n",
       "      <td>105.000000</td>\n",
       "      <td>105.100000</td>\n",
       "      <td>999.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         student_id     Chinese        Math     English\n",
       "count    121.000000  115.000000  112.000000  121.000000\n",
       "mean   23060.090909   80.170435   80.370536   93.771901\n",
       "std       34.930884    8.977525   11.157364  118.098218\n",
       "min    23001.000000   58.800000   54.600000   57.200000\n",
       "25%    23030.000000   74.200000   71.875000   73.000000\n",
       "50%    23060.000000   79.300000   81.900000   78.500000\n",
       "75%    23090.000000   85.500000   88.125000   83.800000\n",
       "max    23120.000000  105.000000  105.100000  999.000000"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df = pd.read_csv('./data/students_raw_utf8.csv',encoding='utf-8-sig')\n",
    "df.head(10)#显示开始的10条数据\n",
    "df.tail(10)#显示尾部的10条数据\n",
    "display(df.sample(10,random_state=100))\n",
    "#可以有针对性的读取一些数据\n",
    "df = pd.read_csv('./data/students_raw_utf8.csv',encoding='utf-8',usecols={'student_id','name','Chinese','Math','English'})\n",
    "display(df)\n",
    "display(df.info())\n",
    "display(df.describe())\n",
    "df.to_csv('data/student_score.csv',index=False,encoding='utf-8-sig')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
